<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 刘洪兆0720
  Date: 2022/5/1
  Time: 22:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" isErrorPage="true">
    <title>上机考试系统</title>

</head>
<style>
    body {
        background-color: #f8f6e9;
    }
</style>
<body>
<%
    if (session.getAttribute("info") != null) {
        out.print(session.getAttribute("info"));
        session.removeAttribute("info");
    }
%>
<jsp:include page="student_head.jsp"></jsp:include>
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h3>
                <strong>试卷下载</strong>
            </h3>
            <div class="alert"
                 style="display:block;background-color: gray; margin-top: 5px">
                <span style="color: white;">本次考试没有电子试卷</span>
            </div>
            <a class="btn btn-default"
               href="${path}/examDownload?e_name=${student.stu_exam}&e_teacher=${StartExamTeaName}"
               style="display:block">下载查看</a>
        </div>
        <div class="col-md-6">
            <h3>
                <strong>答案上传</strong>
            </h3>
            <span style="font-size: 18px">请按照考试要求将答案文件打包，再次进行上传。同名文件多次上传将会覆盖。</span>
            <form class="form-inline" name="file" role="form"
                  action="${path}/saveAnswer" onsubmit="return saveExam()" enctype="multipart/form-data"
                  method="post" style="margin-top: 12px; font-size: 18px">
                <input type="file" id="paper" name="paper">
                <input type="hidden" name="stu_id" value="${student.stu_id}">
                <input type="hidden" name="stu_exam" value="${student.stu_exam}">
                <button type="submit" style="margin-top: 10px;" class="btn btn-info">上传</button>
            </form>
        </div>
    </div>

    <!-- 显示表格数据 -->
    <div class="row" style="margin-top: 30px">
        <div class="col-md-12">
            <strong style="display:block;margin-left: 20px; font-size: 18px">全部系统通知</strong>
            <!--分页文字信息  -->
            <div class="col-md-8 text-start" id="page_info_area"
                 style="margin-bottom: 10px;margin-left:5px;margin-top:20px;font-size: 15px;"></div>

            <table class="table table-responsive table-bordered table-striped table-hover" id="teas_table">
                <thead class="table-light">
                <tr>
                    <th class="col-md-3">通知内容</th>
                    <th class="col-md-3">通知时间</th>
                    <th class="col-md-3">相关考试</th>
                    <th class="col-md-3">发布者</th>
                </tr>
                </thead>
                <tbody id="tbody">

                </tbody>
            </table>
        </div>
    </div>

    <!-- 显示分页信息 -->
    <div class="row" style="margin-top: 100px;align-content: center">
        <!-- 分页条信息 -->
        <div class="col-md-12" id="page_nav_area" style="align-self: center"></div>
    </div>
</div>
</body>
<script type="text/javascript">
    var currentPage, totalRecord;
    //1、页面加载完成以后，直接去发送ajax请求,要到分页数据
    $(function () {
        //去首页
        to_page(1);
    });

    function saveExcel() {
        if ($("input[name='file']").val().length == 0) {
            alert("请选择文件");
            return false;
        }
        return true;
    }

    function to_page(pageNum) {
        $.ajax({
            url: "${path}/getAllInfosOfStu",
            data: {"pageNum": pageNum},
            type: "GET",
            success: function (pageInfo) {
                //1、解析并显示教师数据
                buildTable(pageInfo);
                //2、解析并显示分页信息
                build_page_info(pageInfo);
                //3、解析显示分页条数据
                build_page_nav(pageInfo);
            }
        });
    }

    function buildTable(pageInfo) {
        //清空table表格
        $("#teas_table tbody").empty();
        var data = pageInfo.extend.pageInfo.list;
        var result = "";
        for (var i = 0; i < data.length; i++) {
            result += "<tr>";
            result += "<td>" + data[i].info + "</td>";
            result += "<td>" + jsDateFormat(data[i].times) + "</td>";
            result += "<td>" + data[i].exam + "</td>";
            result += "<td>" + data[i].t_name + "</td>";
            result += "</tr>";
        }
        $("#tbody").html(result);
    }

    function jsDateFormat(time) {
        var date = new Date(time);
        var year = date.getFullYear();
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        // 拼接
        return year + "-" + month + "-" + day + "-" + hours + "-" + minutes + "-" + seconds;
    }


    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前" + result.extend.pageInfo.pageNum + "页,总共" +
            result.extend.pageInfo.pages + "页,总共" +
            result.extend.pageInfo.total + "条记录");
        totalRecord = result.extend.pageInfo.total;
        currentPage = result.extend.pageInfo.pageNum;
    }

    //解析显示分页条，点击分页要能去下一页....
    function build_page_nav(result) {
        //page_nav_area
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");

        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href", "#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if (result.extend.pageInfo.hasPreviousPage == false) {
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");
        } else {
            //为元素添加点击翻页的事件
            firstPageLi.click(function () {
                to_page(1);
            });
            prePageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum - 1);
            });
        }

        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href", "#"));
        if (result.extend.pageInfo.hasNextPage == false) {
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        } else {
            nextPageLi.click(function () {
                to_page(result.extend.pageInfo.pageNum + 1);
            });
            lastPageLi.click(function () {
                to_page(result.extend.pageInfo.pages);
            });
        }

        //添加首页和前一页 的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2，3遍历给ul中添加页码提示
        $.each(result.extend.pageInfo.navigatepageNums, function (index, item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if (result.extend.pageInfo.pageNum == item) {
                numLi.addClass("active");
            }
            numLi.click(function () {
                to_page(item);
            });
            ul.append(numLi);
        });
        //添加下一页和末页 的提示
        ul.append(nextPageLi).append(lastPageLi);

        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }
</script>
</html>